package com.ironman.imes.device.framework.web.handler;

import com.ironman.imes.device.framework.base.Res;
import com.ironman.imes.device.framework.base.enums.IErrorCode;
import com.ironman.imes.device.framework.base.exception.ClientException;
import com.ironman.imes.device.framework.base.exception.RemoteException;
import com.ironman.imes.device.framework.base.exception.ServiceException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.multipart.MaxUploadSizeExceededException;
import org.springframework.web.servlet.NoHandlerFoundException;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * description
 * company 铁人科技
 *
 * @author 陈浩杰
 * @date 2022/8/17 8:31
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(MaxUploadSizeExceededException.class)
    public Res onException(MaxUploadSizeExceededException e) {
        return Res.error(IErrorCode.OSS_1002001);
    }

    @ExceptionHandler(BindException.class)
    public Res onException(BindException e) {
        List<FieldError> fieldErrors = e.getBindingResult().getFieldErrors();
        String message = fieldErrors.stream().map(n -> n.getField() + ":" + n.getDefaultMessage()).collect(Collectors.joining(";"));
        return Res.error(IErrorCode.SYS_2000023, message);
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Res onException(MethodArgumentNotValidException e) {
        List<FieldError> fieldErrors = e.getBindingResult().getFieldErrors();
        String message = fieldErrors.stream().map(n -> n.getField() + ":" + n.getDefaultMessage()).collect(Collectors.joining(";"));
        return Res.error(IErrorCode.SYS_2000022, message);
    }

    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public Res onException(HttpRequestMethodNotSupportedException e) {
        return Res.error(IErrorCode.SYS_2000021);
    }

    @ExceptionHandler(HttpMediaTypeNotSupportedException.class)
    public Res onException(HttpMediaTypeNotSupportedException e) {
        return Res.error(IErrorCode.SYS_2000020, "MediaType类型【" + e.getContentType() + "】类型错误");
    }

    @ExceptionHandler(NoHandlerFoundException.class)
    @ResponseStatus(HttpStatus.NOT_FOUND)
    public Res onException(NoHandlerFoundException e, HttpServletRequest request) {
        return Res.error(IErrorCode.SYS_2000025, "[" + request.getRequestURI() + "]访问资源不存在");
    }

    @ExceptionHandler(ClientException.class)
    public Res onException(ClientException e) {
        return Res.error(e.getCode(), e.getMessage());
    }

    @ExceptionHandler(RemoteException.class)
    public Res onException(RemoteException e) {
        return Res.error(e.getCode(), e.getMessage());
    }

    @ExceptionHandler(ServiceException.class)
    public Res onException(ServiceException e) {
        return Res.error(e.getCode(), e.getMessage());
    }

    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public Res onException(Exception e) {
        return Res.error(IErrorCode.SYS_2000001, "系统发生未知异常:" + e.getMessage());
    }
}
